package com.aishua.aishuawallet.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.bean.ReqTransBean;
import com.aishua.aishuawallet.bean.ReqTransnation;
import com.aishua.aishuawallet.utils.AndroidUtils;
import com.aishua.aishuawallet.utils.LogUtils;
import com.aishua.aishuawallet.utils.TimeUtil;

import org.spongycastle.jcajce.provider.symmetric.Serpent;
import org.web3j.utils.Convert;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import app.socketlib.com.library.utils.LogUtil;

/**
 * Created by aishua on 2018/4/3.
 */

public class TransactionAdapter extends RecyclerView.Adapter<TransactionAdapter.MyDeviceViewHoler> {

    private Context mContext;
    private ReqTransnation reqTransnation;

    public TransactionAdapter(Context mContext, ReqTransnation reqTransnation) {

        this.mContext = mContext;
        this.reqTransnation = reqTransnation;
        LogUtils.v("reqTransnation:" + reqTransnation.toString());
    }

    @Override
    public MyDeviceViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.wallet_latelytra, parent, false);
        return new MyDeviceViewHoler(view);

    }

    @Override
    public void onBindViewHolder(MyDeviceViewHoler holder, final int position) {

        holder.displayView(position);
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                onRecycleViewClickListener.onRecycleViewClick(position);
//            }
//        });

    }

    @Override
    public int getItemCount() {
        if (null == reqTransnation) {

            return 0;
        }
        return reqTransnation.getResult().size();

//        return  3;
    }

    public class MyDeviceViewHoler extends RecyclerView.ViewHolder {

        private TextView tv_address;
        private TextView tv_money;
        private TextView tv_tratime;
        private ImageView img_transfer;

        public MyDeviceViewHoler(View itemView) {
            super(itemView);
            tv_address = itemView.findViewById(R.id.tv_address);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_tratime = itemView.findViewById(R.id.tv_tratime);
            img_transfer = itemView.findViewById(R.id.img_transfer);
        }

        public void displayView(int position) {

            String from = reqTransnation.getResult().get(position).getFrom();
            String value = reqTransnation.getResult().get(position).getValue();
            String timeStamp = reqTransnation.getResult().get(position).getTimeStamp();

            String valuewei = String.format("%.4f", Convert.fromWei(value, Convert.Unit.ETHER));
            String  date = TimeUtil.getTimeFormatText(timeStamp);
            LogUtils.v("data:"+date);
            Drawable drawable;
            int color;
            if ("0x3907d69517b58355207e9a371add9dbc1e198a5b".equals(from)) {

                drawable = mContext.getDrawable(R.drawable.transferin);
                valuewei = "-"+ valuewei;
                color = mContext.getResources().getColor(R.color.colorAccent);

            } else {

                drawable = mContext.getDrawable(R.drawable.transferout);
                valuewei = "+"+ valuewei;
                color =mContext.getResources().getColor(R.color.deepblue);

            }

            tv_address.setText(from);
            tv_money.setText(valuewei+" eth");
            tv_money.setTextColor(color);
            tv_tratime.setText(date);
            img_transfer.setBackground(drawable);


        }

    }


    public void setonRecycleViewClickListener(onRecycleViewClickListener onRccycleViewLongClickListener) {
        this.onRecycleViewClickListener = onRccycleViewLongClickListener;
    }

    public void setOnRccycleViewLongClickListener(onRccycleViewLongClickListener onRccycleViewLongClickListener) {

        this.onRccycleViewLongClickListener = onRccycleViewLongClickListener;
    }

    private onRecycleViewClickListener onRecycleViewClickListener;
    private onRccycleViewLongClickListener onRccycleViewLongClickListener;

    public interface onRecycleViewClickListener {

        public void onRecycleViewClick(int position);
    }

    public interface onRccycleViewLongClickListener {

        public void onRccycleViewLongClick();
    }


}